package com.example.tool.dao.util;

import com.example.tool.MyApplication;
import com.example.tool.dao.DaoManager;
import com.example.tool.dao.entity.Person;
import com.example.tool.greendao.PersonDao;
import com.example.tool.util.CalendarUtil;
import com.example.tool.util.DataRequest;
import com.example.tool.util.StringUtil;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;

import static com.example.tool.util.CalendarUtil.DATE_FORMAT_YMD;
import static com.example.tool.util.DataRequest.getGender;

/**
 * Created Date 2019/9/27
 *
 * @author liangkun
 * description：人物工具类
 */
public class PersonDaoUtil {
    private static PersonDaoUtil personDaoUtil;

    /**
     * @param
     * @return
     * @description
     * @author liangkun
     * @time 2019/9/27 14:33
     */
    public static PersonDaoUtil getPersonUtil() {
        if (null == personDaoUtil) {
            synchronized (PersonDaoUtil.class) {
                if (null == personDaoUtil) {
                    personDaoUtil = new PersonDaoUtil();
                }
            }
        }
        return personDaoUtil;
    }


    /**
     * @return PersonDao
     * @description
     * @author liangkun
     * @time 2019/9/27 14:34
     */
    public PersonDao getPersonDao() {
        return DaoManager.getInstance(MyApplication.getContext()).getDaoSession().getPersonDao();
    }

    /**
     * @param
     * @return
     * @description 插入数据
     * @author liangkun
     * @time 2019/9/29 14:25
     */
    public int insertPerson(Person person) {
        return (int) getPersonDao().insert(person);
    }

    /**
     * @param
     * @return
     * @description 获取所有的数据
     * @author liangkun
     * @time 2019/9/29 14:27
     */
    public List<Person> getAllPerson() {
        return getPersonDao().loadAll();
    }

    /**
     * @param
     * @return
     * @description 插入名字的人
     * @author liangkun
     * @time 2019/9/29 14:54
     */
    public Person insertAddPerson(String personName) {
        Person person = new Person();
        person.setPersonId(StringUtil.getUuid());
        String currentDate = CalendarUtil.getCurrentDate(DATE_FORMAT_YMD);
        person.setPersonBirthday(currentDate);
        person.setPersonGender(getGender(DataRequest.genderList));
        person.setPersonYear(CalendarUtil.getYearFromDate(currentDate));
        person.setPersonMonth(CalendarUtil.getMonthFromDate(currentDate));
        person.setPersonDay(CalendarUtil.getDayFromDate(currentDate));
        person.setPersonAge(CalendarUtil.getAge(currentDate));
        person.setPersonName(personName);
        int insert = insertPerson(person);
        if (insert > 0) {
            return person;
        }
        return null;
    }


    /**
     * @param
     * @return
     * @description 查询人名
     * @author liangkun
     * @time 2019/9/29 10:51
     */
    public List<Person> queryPerson(String personName) {
        QueryBuilder<Person> builder = getPersonDao().queryBuilder();
        WhereCondition condition = PersonDao.Properties.PersonName.eq(personName);
        return builder.where(condition).orderDesc(PersonDao.Properties.PersonAge).list();

    }

    /**
     * @param
     * @return
     * @description 模糊查询
     * @author liangkun
     * @time 2019/9/29 10:51
     */
    public List<Person> queryLikePerson(String likeName) {
        QueryBuilder<Person> builder = getPersonDao().queryBuilder();
        WhereCondition condition = PersonDao.Properties.PersonName.like("%" + likeName + "%");
        return builder.where(condition).orderDesc(PersonDao.Properties.PersonAge).list();
    }

    /**
     * @param
     * @return
     * @description 区间查询 在某个值内
     * @author liangkun
     * @time 2019/9/29 16:09
     */
    public List<Person> getRangePerson(String startyear, String endyear) {
        QueryBuilder<Person> builder = getPersonDao().queryBuilder();
        WhereCondition condition = PersonDao.Properties.PersonYear.in(startyear, endyear);
        return builder.where(condition).orderAsc(PersonDao.Properties.PersonBirthday).list();
    }

    /**
     * @param
     * @return
     * @description 区间查询 在某个值内[单个 =] 多个就是区间
     * @author liangkun
     * @time 2019/9/29 16:09
     */
    public List<Person> getInPerson(String age) {
        QueryBuilder<Person> builder = getPersonDao().queryBuilder();
        WhereCondition condition = PersonDao.Properties.PersonAge.in(age);
        return builder.where(condition).orderDesc(PersonDao.Properties.PersonMonth).list();
    }

    /**
     * @param
     * @return
     * @description 大于某个年龄 [大于必须要用int 类型]
     * @author liangkun
     * @time 2019/9/29 16:09
     */
    public List<Person> getGtAgePerson(String age) {
        QueryBuilder<Person> builder = getPersonDao().queryBuilder();
        WhereCondition condition = PersonDao.Properties.PersonAge.gt(Integer.valueOf(age));
        return builder.where(condition).orderDesc(PersonDao.Properties.PersonAge).list();
    }

    /**
     * orderRaw("random()") //随机排序
     *
     * @param
     * @return
     * @description 查询限制数据
     * @author liangkun
     * @time 2019/10/10 11:09
     */
    public List<Person> getLimitRefesh(int page) {
        int start = 30 * (page - 1);
        QueryBuilder<Person> builder = getPersonDao().queryBuilder();
        return builder.orderDesc(PersonDao.Properties.PersonAge).offset(start).limit(30).list();
    }

    /**
     * @param
     * @return
     * @description 获取数量
     * @author liangkun
     * @time 2019/10/10 11:32
     */
    public int getPersonCount() {

        return getAllPerson().size();
    }
}
